package com.junlong0716.enjoylearning.ui

import android.graphics.Color
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.adapter.FindMultipleItemAdapter
import com.junlong0716.enjoylearning.entity.*
import com.junlong0716.getknowledgedemo.decoration.DividerItemDecoration
import com.junlong0716.module.common.base.BaseFragment
import android.app.ActivityOptions
import android.content.Intent
import android.os.Build
import android.widget.Toast
import com.blankj.utilcode.util.ToastUtils
import com.junlong0716.enjoylearning.HomeActivity
import com.scwang.smartrefresh.layout.SmartRefreshLayout


/**
 *@author: 巴黎没有摩天轮Li
 *@description:
 *@date: Created in 上午11:53 2018/4/21
 *@modified by:
 */
class FindFragment : BaseFragment<FindPresenter>(), FindContract.View, BaseQuickAdapter.OnItemChildClickListener {
    private var overallXScroll = 0//用来记住滑动的距离
    private lateinit var llSearch: LinearLayout
    private lateinit var rvList: RecyclerView
    private lateinit var rlTextBg: RelativeLayout
    private lateinit var findMultipleItemAdapter: FindMultipleItemAdapter
    private lateinit var viewDivider: View
    private val height = 440// 滑动开始变色的高,真实项目中此高度是由广告轮播或其他首页view高度决定
    private lateinit var data: ArrayList<FindMultipleItemEntity>

    private lateinit var bannerData: ArrayList<FindBannerEntity>
    private lateinit var headerData: ArrayList<FindItemEntity>

    private lateinit var commendTopicData: ArrayList<FindItemEntity>
    private lateinit var commendTopicItemData: ArrayList<FindCommendTopicEntity>

    private lateinit var commendVideoData: ArrayList<FindItemEntity>
    private lateinit var commendVideoItemData: ArrayList<FindCommendVideoEntity>

    private lateinit var commendAudioData: ArrayList<FindItemEntity>
    private lateinit var commendAudioItemData: ArrayList<FindCommendAudioEntity>

    private lateinit var commendDocumentData: ArrayList<FindItemEntity>
    private lateinit var commendDocumentItemData: ArrayList<FindCommendDocumentEntity>

    private lateinit var swipeRefreshLayout: SmartRefreshLayout
    private var isChange = false //是否是换一换
    private var isChangePosition = 0 //记录是刷新哪一个item

    override fun getLayoutId(): Int = R.layout.fragment_find

    override fun initViews(mRootView: View?) {


        llSearch = mRootView!!.findViewById<View>(R.id.ll_search) as LinearLayout


        val rlRoot = mRootView.findViewById<LinearLayout>(R.id.rl_root)

        rlRoot.isFocusableInTouchMode = true
        rlRoot.requestFocus()

        rvList = mRootView.findViewById(R.id.rv_list)
        rlTextBg = mRootView.findViewById(R.id.rl_text_bg)
        viewDivider = mRootView.findViewById(R.id.view_divider)
        swipeRefreshLayout = mRootView.findViewById(R.id.srl_refresh)

        data = ArrayList()
        headerData = ArrayList()
        bannerData = ArrayList()
        commendTopicData = ArrayList()
        commendTopicItemData = ArrayList()
        commendVideoData = ArrayList()
        commendVideoItemData = ArrayList()

        commendAudioData = ArrayList()
        commendAudioItemData = ArrayList()

        commendDocumentData = ArrayList()
        commendDocumentItemData = ArrayList()

        rvList.layoutManager = LinearLayoutManager(activity)
        rvList.addItemDecoration(DividerItemDecoration(activity!!.applicationContext, LinearLayoutManager.VERTICAL, 30, ContextCompat.getColor(activity!!, R.color.recyclerview_divider_gray)))

        findMultipleItemAdapter = FindMultipleItemAdapter(data, object : FindMultipleItemAdapter.ItemChildClickListener {
            override fun setOnAudioItemChildClickListener(audioEntity: AudioEntity) {
                (activity as HomeActivity).getPlayerManager().playMusic(audioEntity)

            }
        })

        rvList.adapter = findMultipleItemAdapter

        setTitleBarTransparency()

        setOnRecyclerViewScrollDirectListener(rvList)

        findMultipleItemAdapter.onItemChildClickListener = this

        mRootView.findViewById<LinearLayout>(R.id.ll_search).setOnClickListener {
            val intent = Intent(activity!!, SearchActivity::class.java)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(activity, mRootView.findViewById<LinearLayout>(R.id.ll_search), "search_view").toBundle())
            } else {
                startActivity(intent)
            }
        }
        swipeRefreshLayout.isEnableLoadMore = false

        swipeRefreshLayout.setOnRefreshListener {

            data.clear()
            isChange = false
            mPresenter!!.requestBanner(this)

        }
    }

    override fun lazyFetchData() {
        swipeRefreshLayout.autoRefresh()
    }

    override fun attachPresenter() {
        mPresenter = FindPresenter()
        mPresenter!!.attachView(this)
    }


    //滑动 search bar 背景颜色渐变
    private fun setTitleBarTransparency() {
        rvList.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                overallXScroll += dy// 累加y值 解决滑动一半y值为0
                when {
                    overallXScroll <= 0 -> {
//                        //设置标题的背景颜色
//                        llSearch.setBackgroundColor(Color.argb(255, 255, 255, 255))
//                        //Glide.with(activity).load(R.mipmap.download_white).into(iv_download)
//                        viewDivider.setBackgroundColor(Color.argb(255, 229, 229, 229))
//                        rlTextBg.setBackgroundResource(R.drawable.shape_search_bg_white)
                    }
                    overallXScroll in 1..height -> {
//                        //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
//                        val scale = overallXScroll.toFloat() / height
//                        val alpha = 255 * scale
//                        llSearch.setBackgroundColor(Color.argb(255, 255, 255, 255))
                    }
                    else -> {
//                        llSearch.setBackgroundColor(ContextCompat.getColor(activity!!, R.color.common_transparent100_white))
//                        //Glide.with(activity).load(R.mipmap.download).into(iv_download)
//                        viewDivider.setBackgroundColor(Color.argb(255, 229, 229, 229))
//                        rlTextBg.setBackgroundResource(R.drawable.shape_search_bg)
                    }
                }
            }
        })
    }


    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        when (view!!.id) {
            R.id.ll_classify -> {
                go(ClassifyActivity::class.java)
            }
            R.id.ll_document -> {
                ChannelActivity.newInstance(activity!!, "文稿", 3)
            }

            R.id.ll_video -> {
                ChannelActivity.newInstance(activity!!, "视频", 1)
            }
            R.id.ll_audio -> {
                ChannelActivity.newInstance(activity!!, "音频", 2)
            }

            R.id.rl_change -> {
                //ToastUtils.showShort(position.toString())
                isChange = true
                isChangePosition = position
                mPresenter!!.requestCommandKnowledge(true, this)
            }
        }
    }

    override fun requestCommandKnowledgeSucceed(commendVideoData: ArrayList<FindItemEntity>, commendAudioData: ArrayList<FindItemEntity>, commendDocumentData: ArrayList<FindItemEntity>, commendTopicData: ArrayList<FindItemEntity>) {
        //data.add(FindMultipleItemEntity(FindMultipleItemEntity.FIND_TYPE_HEADER, headerData))
        if (isChange) {

            when (isChangePosition) {
                0 -> {
//                    data[0] = FindMultipleItemEntity(FindMultipleItemEntity.FIND_TYPE_COMMEND_AUDIO, commendAudioData)
//                    findMultipleItemAdapter.notifyItemChanged(isChangePosition)
                }

                1 -> {
                    data[1] = FindMultipleItemEntity(FindMultipleItemEntity.FIND_TYPE_COMMEND_AUDIO, commendAudioData)
                    findMultipleItemAdapter.notifyItemChanged(isChangePosition)
                }
                2 -> {
                    data[2] = FindMultipleItemEntity(FindMultipleItemEntity.FIND_TYPE_COMMEND_DOCUMENT, commendDocumentData)
                    findMultipleItemAdapter.notifyItemChanged(isChangePosition)
                }
                3 -> {
                    data[3] = FindMultipleItemEntity(FindMultipleItemEntity.FIND_TYPE_COMMEND_VIDEO, commendVideoData)
                    findMultipleItemAdapter.notifyItemChanged(isChangePosition)
                }
            }
            //data.add(FindMultipleItemEntity(FindMultipleItemEntity.FIND_TYPE_COMMEND_TOPIC, commendTopicData))


            //swipeRefreshLayout.finishRefresh()
        } else {
            data.add(FindMultipleItemEntity(FindMultipleItemEntity.FIND_TYPE_COMMEND_AUDIO, commendAudioData))
            data.add(FindMultipleItemEntity(FindMultipleItemEntity.FIND_TYPE_COMMEND_DOCUMENT, commendDocumentData))
            data.add(FindMultipleItemEntity(FindMultipleItemEntity.FIND_TYPE_COMMEND_VIDEO, commendVideoData))
            data.add(FindMultipleItemEntity(FindMultipleItemEntity.FIND_TYPE_COMMEND_TOPIC, commendTopicData))
            findMultipleItemAdapter.notifyDataSetChanged()
            swipeRefreshLayout.finishRefresh()
        }
    }

    override fun requestBannerSucceed(headerData: ArrayList<FindItemEntity>) {
        data.add(FindMultipleItemEntity(FindMultipleItemEntity.FIND_TYPE_HEADER, headerData))
        mPresenter!!.requestCommandKnowledge(true, this)
    }
}
